<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Blovex4\Controller;
use Blovex4\Logic\AdminLogic;

/**
 * 角色控制器
 * @author derek <693472824@qq.com>
 */
class RoleController extends BaseController {

    /**
     * 获取角色列表
     * @author derek <693472824@qq.com>
     */
    public function index () {
        
        $rolename = I('rolename');
        
        $where = array();
        
        if(!empty($rolename)){
        
            $where['role_name'] = array('like', '%'.(string)$rolename.'%');
        }
        
        $roleModel = M('Role');
        $list = $this->lists($roleModel, $where);
        
        $this->assign('rolename', $rolename);
        $this->assign('rolelist', $list);
        $this->display();
    }

    /**
     * 增加用户以及修改用户
     *
     * @param integer $admin_id
     *            用户ID.
     * @author derek <693472824@qq.com>
     */
    public function editAction() {
        
        $role_id = I('role_id');
    
        if (!empty($role_id)) {
    
            $roleLogic = D('Role', 'Logic');
            $roleModel = $roleLogic->getRoleByID($role_id);
    
            if (empty($roleModel)) {
                $this->error('获取数据失败', U('index'));
            } else {
                $this->assign('roleModel', $roleModel);
            }
        }
    
        $this->display();
    }
    
    /**
     * 保存用户信息
     *
     * @author derek <lixm@my818.com>
     */
    public function saveAction() {
    
        $roleLogic = D('role', 'Logic');
        $result = $roleLogic->saveAction();
    
        if ($result === false) {
            $this->error('操作失败' . $roleLogic->getError());
        } else {
            $this->success('操作成功', U('index'));
        }
    }

    /**
     * 角色删除
     * @author derek <693472824@qq.com>
     */
    public function del() {
        
        $role_ids = I('roleids');
        
        if(empty($role_ids)){
            $this->error('参数错误！');
        }
        
        $roleLogic = D('Role', 'Logic');
        $result = $roleLogic->del($role_ids);
        
        if ($result === false) {
            $this->error('参数错误', U('index'));
        } else {
            $this->success('操作成功！', U('index'));
        }
    }
    
    /**
     * 设置角色权限
     * @author derek <693472824@qq.com>
     */
    public function authority() {
        
        $role_id = I('role_id');
        
        if(empty($role_id)){
            $this->error('参数错误!');
        }
        
        $roleLogic = D('Role', 'Logic');
        
        if(IS_POST){
            
            $menu_id_str = I('menuid');
            $result = $roleLogic->updateAuth($role_id, $menu_id_str);
            
            if($result === false){
                $this->error('操作失败');
            }else{
                $adminid = AdminLogic::is_login();
                session('ADMIN_MENU' . $adminid, null);
                $this->success('操作成功', U('index'));
            }
        }else{//获取角色对应的菜单树
            
            $roleModel = $roleLogic->getRoleByID($role_id);
            $role_name = $roleModel['role_name'];
            
            $menu_tree = D('Role', 'Logic')->getAuthByRoleID($role_id);
            
            $this->assign('role_name', $role_name);
            $this->assign('role_id', $role_id);
            $this->assign('__AUTH__', $menu_tree);
            $this->display();
        }
    }
}